﻿(function () {
  'use strict';
  var controllerId = 'newproject';
  angular.module('ng4model').controller(controllerId, ['common', '$http', '$scope', '$location', '$routeParams', 'currents', 'datacontext', projects]);

  function projects(common, $http, $scope, $location, $routeParams, currents, datacontext) {
    var getLogFn = common.logger.getLogFn;
    var log = getLogFn(controllerId);
    // store forwarded parameters
    var opusId = $routeParams.id;

    // View Model
    var vm = this;
    vm.title = 'New Project';
    vm.newProject = {};
    activate();

    function activate() {
      var promises = [];
      common.activateController(promises, controllerId)
          .then(function () { log('Activated New Projects View'); });
    }

    // Handler
    $scope.cancel = function () {
      $location.url("/projects");
    };

    $scope.newProject = function (sample) {
      if (!sample) {
        if ($scope.hasValidator) {
          // if is valid at client save to server
          if ($scope.validate()) {
            //regular
            datacontext.addProject(JSON.stringify(vm.newProject))
              .then(function (data) {
                toastr.successs(data.msg);
              }).catch(function (data) {
                toastr.error(data.msg);
              });
          }
        }
      } else {
        // sample (instructions to work with privately)
        datacontext.addSampleProject()
          .then(function (data) {
            toastr.successs(data.msg);
          }).catch(function (data) {
            toastr.error(data.msg);
          });
      }
    };


  }
})();